import { segment } from "oicq";
import fetch from "node-fetch";
//v2改的，v2大佬不知道是谁，但是我非常感谢那位不留名的大佬
//装了就会覆盖闲心的wiki，注意！！！
//功能比闲心多点
const _path = process.cwd();
export class example extends plugin {
  constructor () {
    super({
      /** 功能名称 */
      name: 'wiki',
      /** 功能描述 */
      dsc: '简单开发示例',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 500,
      rule: [
        {
          /** 命令正则匹配 */
          reg: "^#(W|w)iki[\s\S]*|^第[0-9]+页$",
          /** 执行方法 */
          fnc: 'wiki'
        },
        {
          /** 命令正则匹配 */
          reg: "^[0-9]+$",
          /** 执行方法 */
          fnc: 'list'
        },
      ]
    })
 }



async wiki(e) {
  let msg = "";
    if (e.msg.slice(1,2) == "w"){ 
       text = e.msg.replace("#wiki","").replace(/(^\s*)/g, "");
    } else if (e.msg.slice(1,2) == "W"){
        text = e.msg.replace("#Wiki","").replace(/(^\s*)/g, "");
    }
  if(text == "")return;
  if(e.isGroup){e.group.recallMsg(message_id);}
  if(e.msg.search(/页/) != -1){ 
    page = e.msg.replace("第","").replace("页", "");
  }
  try{
  let lolicon = `https://api-takumi.mihoyo.com/common/blackboard/ys_obc/v1/search/content?app_sn=ys_obc&keyword=${text}&page=${page}`;
  let response = await fetch(lolicon);
      res = await response.json(); 
  let pages = Math.ceil(res.data.total/15)
  let list = res.data.list;
  for (var i = 1; i <= list.length; i++) {
      let title = list[i-1].title;
      let summary = list[i-1].summary;
      msg = msg.concat(`${i}、${title}-${summary}\n`)
  }
  msg = msg.concat(`当前第${page}页，总共${pages}页\n请回复对应序号！\n15秒之内有效。`)
  wikizt = 1;
  setTimeout(() => {if(e.isGroup){e.group.recallMsg(message_id);}wikizt = 0;text ="";page = 1;}, 15000);
  } catch(err) {
    msg = [
      "出错了！请重试！",
    ];
  }
  let msgRes = await e.reply(msg);
  message_id = msgRes.message_id
  return true;
}
async list(e) {
    if (wikizt != 1)return;
    let msg;
    try{
        let data = res.data.list[e.msg-1];
        let title = data.title;
        let icon = data.icon;
        let url = data.bbs_url

        msg = [
            "为您找到：",title,"\n",
            segment.image(icon),"\n",
            url,
        ];

    } catch(err) {
        msg = [
           "出错了！请重试！",
         ];
    }
    e.reply(msg);
    text ="";
    wikizt = 0;
    page = 1;
    return true;
}

}
let text = "";
let page = 1;
let wikizt = 0;
let message_id;
let res;

